//
//  SupperCollectCell.m
//  AutoHome
//
//  Created by Flying meat on 9/11/15.
//  Copyright (c) 2015 CaoFeng. All rights reserved.
//

#import "SupperCollectCell.h"
#import "CFtableViewCell.h"
#import "UIImageView+AFNetworking.h"
#import "DetailViewController.h"
#import "RecommendViewController.h"
@implementation SupperCollectCell

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _taparry=[NSMutableArray new];
    }
    return self;
}


#pragma mark -UITableViewDelegate,UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  
  
    NSArray *arry=[NSArray new];
   
    NSArray *arry2=[NSArray new];
    NSDictionary*headinfo=[NSDictionary new];
    for (NSDictionary *dic in self.allArray) {
        
        arry=dic[@"newslist"];
        
        arry2=dic[@"listArray"];
    

        headinfo=dic[@"headlineinfo"];
    
    
    }
    if (arry2.count!=0) {
        return arry2.count;
    }
    
    return arry.count;
    
    
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    CFtableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"CFtableViewCell"];


    
    //解决imgView不回到原位置的bug
    cell.imgVeiw.frame=CGRectMake(8, 8, 106, 67);
    cell.timeLabel.frame=CGRectMake(122, 54, 76, 21);
    cell.contextLabel.frame=CGRectMake(122, 8, 198, 46);
    cell.answerLabel.frame=CGRectMake(243, 54, 77, 21);
    self.myTableView.tableHeaderView=NULL;
    [self.myTableView.tableHeaderView removeFromSuperview];
    self.imageVieww=NULL;
    self.page=NULL;
    [self.page removeFromSuperview];
    self.page.hidden=YES;
    self.scrollview=NULL;
    [_timer invalidate];
    _timer=NULL;
    
    cell.otherLabel.hidden=YES;
    cell.playingLabel.hidden=YES;
    NSArray *arry=[NSArray new];
    NSArray *videoArray=[NSArray new];
    NSArray *focusimgArray=[NSArray new];
    NSDictionary*headinfo=[NSDictionary new];
    for (NSDictionary *dic in self.allArray) {
        arry=dic[@"newslist"];

        focusimgArray=dic[@"focusimg"];
        videoArray=dic[@"listArray"];

        headinfo=dic[@"headlineinfo"];
    }
    
    
    if (videoArray.count!=0) {
        NSDictionary *dic=videoArray[indexPath.row];
        
     
        if (dic[@"smallimg"]==NULL&&dic[@"img"]==NULL) {
            [cell.imgVeiw setImageWithURL:[NSURL URLWithString:dic[@"smallpic"]]];
            cell.timeLabel.text=dic[@"time"];
            cell.answerLabel.text=[NSString stringWithFormat:@"%@评论",dic[@"replycount"]];
            cell.contextLabel.text=dic[@"title"];
        
            
            
            
            
        }else if (dic[@"bgimage"]!=NULL&&dic[@"smallimg"]==NULL) {
            
            cell.otherLabel.hidden=NO;
            cell.playingLabel.hidden=NO;
            
            
            NSNumber *number=dic[@"state"];
            if ([number intValue]==1) {
                cell.playingLabel.text=@"播报中";
                cell.playingLabel.textColor=[UIColor whiteColor];
                
                cell.backgroundColor=[UIColor blueColor];
            }else{
                cell.playingLabel.text=@"播报结束";
                cell.playingLabel.textColor=[UIColor darkGrayColor];
                cell.playingLabel.backgroundColor=[UIColor colorWithRed:179 green:179 blue:186 alpha:0.25];
                
            }

            
            
            
          cell.otherLabel.text=dic[@"typename"];
          cell.otherLabel.textColor=[UIColor blueColor];
          cell.otherLabel.font=[UIFont systemFontOfSize:12];
        
            cell.playingLabel.font=[UIFont systemFontOfSize:12];
            cell.playingLabel.frame=CGRectMake(250, 10, 50, 15);
            
            cell.otherLabel.frame=CGRectMake(10,10, 80, 10);
            cell.imgVeiw.frame=CGRectMake(10,60, 300, 170);
           
            cell.contextLabel.frame=CGRectMake(10, 20,230,30) ;
            cell.answerLabel.frame=CGRectMake(250, 20,70, 30);
            
            cell.timeLabel.frame=CGRectMake(10, 240, 100, 15);
            
            
            
            cell.timeLabel.text=dic[@"createtime"];
            cell.contextLabel.text=dic[@"title"];
            [cell.imgVeiw setImageWithURL:[NSURL URLWithString:dic[@"bgimage"]]];
            cell.answerLabel.text=[NSString stringWithFormat:@"%@观众",dic[@"reviewcount"]];
            
            }
        else{
            [cell.imgVeiw setImageWithURL:[NSURL URLWithString:dic[@"smallimg"]]];
            cell.timeLabel.text=dic[@"time"];
            cell.contextLabel.text=dic[@"title"];
            cell.answerLabel.text=[NSString stringWithFormat:@"%@播放",dic[@"playcount"]];
            
        }
        
       
            return cell;
        
    }
    
    
    
    



   
    
    
    if (focusimgArray.count!=0) {
       
        
        self.myTableView.tableHeaderView.hidden=NO;
    NSMutableArray *_headimgArray=[NSMutableArray new];
        NSString *imgstr=nil;
       
        for(NSDictionary *focusimgDic in focusimgArray){
           imgstr=focusimgDic[@"imgurl"];
            [_taparry addObject:focusimgDic[@"id"]];
        
            [_headimgArray addObject:imgstr];
        }

            
        if (_headimgArray.count==0) {
            return cell;
        }else{
        
        self.myTableView.tableHeaderView=NULL;
        self.scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 44, self.myTableView.frame.size.width, 150)];
        self.scrollview.showsHorizontalScrollIndicator=NO;
        self.scrollview.pagingEnabled=YES;
        self.scrollview.delegate =self;
        self.scrollview.contentSize=CGSizeMake(self.myTableView.frame.size.width*(_headimgArray.count+2), 0);

            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
            
            
            [self.scrollview addGestureRecognizer:tap];
            
        for (int i=0; i<_headimgArray.count+2; i++) {
            self.imageVieww=[[UIImageView alloc]initWithFrame:CGRectMake(i*self.myTableView.frame.size.width, 0, self.myTableView.frame.size.width, 150)];
            
            if (i==0) {
                [self.imageVieww setImageWithURL:[NSURL URLWithString:_headimgArray[5]]];
            }else if (i==7)
            {
                [self.imageVieww setImageWithURL:[NSURL URLWithString:_headimgArray[0]]];
            }else
            {
                
                [self.imageVieww setImageWithURL:[NSURL URLWithString:_headimgArray[i-1]]];
                
            }
            
            self.imageVieww.userInteractionEnabled=YES;
            [self.scrollview addSubview:self.imageVieww];
            
            
        }
        self.scrollview.contentOffset=CGPointMake(self.myTableView.frame.size.width, 0);
        self.page=[[UIPageControl alloc]initWithFrame:CGRectMake(0, 125, self.myTableView.frame.size.width, 30)];
        self.page.numberOfPages=6;
        self.page.backgroundColor=[UIColor clearColor];
        self.page.pageIndicatorTintColor=[UIColor colorWithRed:179 green:179 blue:186 alpha:1];
        
        self.page.currentPageIndicatorTintColor=[UIColor blackColor];
        
        
        
        [self.myTableView.tableHeaderView addSubview:self.page];
        
        
        //给scrollview上tag值
        self.scrollview.tag=400;
        
        
        //给page上tag值
        self.page.tag=401;
        
        
        
        //让视图滚动起来
      _timer=[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(getOut:) userInfo:self.scrollview repeats:YES];
        
        //已经偏移了一个图片大小
        _count=1;
        self.myTableView.tableHeaderView=self.scrollview;
        
        
    
    
    }
    }
    

    
    
    
    
    NSDictionary*dic =arry[indexPath.row];
    
    [cell.imgVeiw setImageWithURL:[NSURL URLWithString:dic[@"smallpic"]]];
    cell.contextLabel.text=dic[@"title"];
    cell.timeLabel.text=dic[@"time"];
    cell.answerLabel.text=[NSString stringWithFormat:@"%@评论",dic[@"replycount"]];


        return cell;
 
    
    
    
}


#pragma mark---根据偏移量来打开headerview
-(void)tap
{
    
    NSLog(@"%f",self.scrollview.contentOffset.x);

    
    



    int n=self.scrollview.contentOffset.x;
    int b=self.scrollview.frame.size.width;
    int c=n/b-1;
    
    NSString * newsid=[NSString stringWithFormat:@"%@",_taparry[c]];
    
    self.callBack(newsid);
    
}



-(void)getOut:(NSTimer *)timer
{
    
    _count++;
    //取得NSTimer所带的参数
    UIScrollView *scroll=timer.userInfo;
    if (_count>=7) {
        
        _count=1;
        //瞬间回到起点
        [scroll setContentOffset:CGPointMake(0, 0)  animated:NO];
        
    }
    NSLog(@"_count==%d",_count);
    //再从起点开始跑
    [scroll setContentOffset:CGPointMake((_count-1)*self.myTableView.frame.size.width,0 ) animated:YES];
//    self.page=(UIPageControl *)[self.myTableView viewWithTag:401];
    self.page.currentPage=_count-1;
    
    
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.tag==400) {
        
        NSLog(@"滑动的是照片");
        //取到tableView上HeadView当前图片的位置
        int index=scrollView.contentOffset.x/self.myTableView.frame.size.width;
        
        //将位置设置给UIpageControl,来实现联动
//        self.page=(UIPageControl *)[self.myTableView viewWithTag:401];
        self.page.currentPage=index-1;
        
        //如果到了第8张,就让它移动到第2张;
        if(index==7)
        {scrollView.contentOffset=CGPointMake(self.myTableView.frame.size.width, 0);
            
            self.page.currentPage=0;
        }
        //如果到了第一张,让他偏移到第7张
        if (index==0) {
            //因为偏移量是从0开始,所以乘以6才是第7张的位置
            scrollView.contentOffset=CGPointMake(self.myTableView.frame.size.width*6, 0);
        }
        
    }
    
    
    
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    NSArray *arry=[NSArray new];
    
    NSArray *arry2=[NSArray new];
    NSDictionary*headinfo=[NSDictionary new];
    for (NSDictionary *dic in self.allArray) {
        
        arry=dic[@"newslist"];
        
        arry2=dic[@"listArray"];
        
        
        headinfo=dic[@"headlineinfo"];
        
        
    }
    NSDictionary *dic=arry2[0];
    NSString *str=dic[@"bgimage"];
    if (str!=NULL) {
        return 270;
    }
    return 84;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *arry=[NSArray new];
    NSArray *videoArray=[NSArray new];
    NSArray *focusimgArray=[NSArray new];
    NSDictionary*headinfo=[NSDictionary new];
    for (NSDictionary *dic in self.allArray) {
        arry=dic[@"newslist"];
        
        focusimgArray=dic[@"focusimg"];
        videoArray=dic[@"listArray"];
        
        headinfo=dic[@"headlineinfo"];
    }
    if (videoArray.count!=0) {
        NSDictionary *dic=videoArray[indexPath.row];
    
        self.newsID=dic[@"id"];
        NSString *newsid=[NSString stringWithFormat:@"%@",self.newsID];
        self.callBack(newsid);
        return;
    }
    NSDictionary*dic =arry[indexPath.row];

        self.newsID=dic[@"id"];
        NSString *newsid=[NSString stringWithFormat:@"%@",self.newsID];
        self.callBack(newsid);
    
    
    
}






@end
